﻿// ***********************************************************************
// Assembly         : AccesoDatos
// Author           : Diego
// Created          : 03-02-2014
//
// Last Modified By : Diego
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="AD_Perfiles.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modelado.Modulos.ClasesModelado;
using System.Data;

/// <summary>
/// The ClasesAccesoDatos namespace.
/// </summary>
namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
    /// <summary>
    /// Class AD_Perfiles.
    /// </summary>
   public class AD_Perfiles
    {

        // Crear Perfils

        /// <summary>
        /// Crs the e_ perfil.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        public void CRE_Perfil(SqlCommand comando, MOD_Perfiles obj)
        {
            try
            {
                string sp = "SP_CRE_Perfil";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                comando.Parameters.Add(new SqlParameter("@PER_nombre", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@PER_descripcion", SqlDbType.VarChar));
             
               

                comando.Parameters[0].Value = obj.PER_nombre;
                comando.Parameters[1].Value = obj.PER_descripcion;
                
                

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


       //elimina Perfil

        /// <summary>
        /// Des the l_ perfil.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        public void DEL_Perfil(SqlCommand comando, MOD_Perfiles obj)
        {
            try
            {
                string sp = "SP_DEL_Perfil";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                comando.Parameters.Add(new SqlParameter("@PER_codperfil", SqlDbType.Int));

                comando.Parameters[0].Value = obj.PER_codperfil;

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        //actualizar Perfil

        /// <summary>
        /// Ups the d_ perfil.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        public void UPD_Perfil(SqlCommand comando, MOD_Perfiles obj)
        {
            try
            {
                string sp = "SP_UPD_Perfil";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@PER_codPerfil", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@PER_nombre", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@PER_descripcion", SqlDbType.VarChar));
                

                comando.Parameters[0].Value = obj.PER_codperfil;
                comando.Parameters[1].Value = obj.PER_nombre;
                comando.Parameters[2].Value = obj.PER_descripcion;
              

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }




        //trae info de Perfil para editar
        /// <summary>
        /// Ses the l_ perfil by identifier.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="PER_CODPerfil">The pe r_ cod perfil.</param>
        /// <returns>MOD_Perfiles.</returns>
        public MOD_Perfiles SEL_PerfilByID(SqlCommand comando, int PER_CODPerfil)
        {
            try
            {
                string sp = "SP_SEL_PerfilByID";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@PER_CODPerfil", SqlDbType.Int));


                comando.Parameters[0].Value = PER_CODPerfil;


                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                MOD_Perfiles obj = new MOD_Perfiles();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    obj.PER_codperfil = Convert.ToInt32(dt.Rows[i]["PERF_CODPERFIL"]);
                    obj.PER_nombre = dt.Rows[i]["PERF_NOMBRE"].ToString();
                    obj.PER_descripcion = dt.Rows[i]["PERF_DESCRIPCION"].ToString();
                    
                }

                return obj;


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }



        /// <summary>
        /// Ses the l_ grid_ perfiles.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <returns>List{MOD_Perfiles}.</returns>
        public List<MOD_Perfiles> SEL_Grid_Perfiles(SqlCommand comando)
        {
            try
            {
                string sp = "SP_SEL_Grid_Perfiles";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_Perfiles> lista = new List<MOD_Perfiles>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_Perfiles obj = new MOD_Perfiles();
                    obj.PER_codperfil = Convert.ToInt32(dt.Rows[i]["PERF_CODPERFIL"]);
                    obj.PER_nombre = dt.Rows[i]["PERF_NOMBRE"].ToString();
                    obj.PER_descripcion = dt.Rows[i]["PERF_DESCRIPCION"].ToString();
                   
                    lista.Add(obj);
                }


                return lista;

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
    }
}
